package query

import (
	"strings"

	"ssdevice/common/dto"
)

type SysDevQuery struct {
	dto.Pagination `search:"-"`
	dto.OrderBy    `search:"-"`
	dto.UserInfo
	IdSn                 string   `form:"idSn" json:"idSn" search:"type:eqOr;column:id,sn;table:sys_dev"`
	Type                 string   `form:"type"  search:"type:exact;column:type;table:sys_dev"`
	SnSearch             string   `form:"sn"  search:"type:contains;column:sn;table:sys_dev"`
	NameSearch           string   `form:"name"  search:"type:contains;column:name;table:sys_dev"`
	Search               string   `form:"search"  search:"type:likeOr;column:sn,name;table:sys_dev"`
	DeptId               string   `form:"deptId" search:"-"`
	DeptIdNot            string   `form:"deptIdNot" search:"-"`
	OnlineStatus         string   `form:"onlineStatus"  search:"type:exact;column:online_status;table:sys_dev"`
	GatewaySn            string   `form:"gatewaySn" search:"-"`
	AlarmStatus          *bool    `form:"alarmStatus"  search:"-"`
	Address              string   `form:"address"  search:"type:contains;column:location;jsonField:address;table:sys_dev"`
	ProductModel         string   `form:"productModel" search:"type:exact;column:product_model;table:sys_dev"`
	ProductModelLike     string   `form:"productModelLike" search:"type:contains;column:product_model;table:sys_dev"`
	IdSnIn               []string `form:"idSnIn" search:"type:inOr;column:id,sn;table:sys_dev"`
	SnIn                 []string `form:"snIn" search:"type:in;column:sn;table:sys_dev"`
	SnNotIn              []string `form:"snNotIn" search:"type:nin;column:sn;table:sys_dev"`
	Sns                  string   `form:"sns" search:"-"`
	TypeNot              string   `form:"typeNot" search:"type:ne;column:type;table:sys_dev"`
	ProductModelIn       []string `form:"productModelIn" search:"type:in;column:product_model;table:sys_dev"`
	ProductModelNotIn    []string `form:"productModelNotIn" search:"type:nin;column:product_model;table:sys_dev"`
	ProductModelInStr    string   `form:"productModelInStr" search:"-"`
	ProductModelNotInStr string   `form:"productModelNotInStr" search:"-"`
	ByProduct            bool     `form:"byProduct" search:"-"`

	GatewaySnIn []string `search:"type:in;column:gateway_sn;table:sys_dev"`
	Sn          string   `search:"type:exact;column:sn;table:sys_dev"`
	GateOrSn    string   `search:"type:eqOr;column:sn,gateway_sn;table:sys_dev"`
	DeptIdIn    []string `search:"type:in;column:dept_id;table:sys_dev"`
	DeptIdNIn   []string `search:"type:nin;column:dept_id;table:sys_dev"`
	NotAlarm    bool     `search:"type:ne;column:alarm_status;table:sys_dev"`
	HasAlarm    bool     `search:"type:exact;column:alarm_status;table:sys_dev"`
}

func (m *SysDevQuery) GetNeedSearch() interface{} {
	if len(m.DeptId) == 0 {
		if m.UserId != "1" {
			m.DeptIdIn = m.DeptIdList
		}
	} else {
		m.DeptIdIn = strings.Split(strings.ReplaceAll(m.DeptId, " ", ""), ",")
	}
	if len(m.GatewaySn) > 0 {
		m.GatewaySnIn = strings.Split(strings.ReplaceAll(m.GatewaySn, " ", ""), ",")
	}
	if m.AlarmStatus != nil {
		if *m.AlarmStatus {
			m.HasAlarm = true
		} else {
			m.NotAlarm = true
		}
	}
	if len(m.Sns) > 0 {
		m.SnIn = strings.Split(strings.ReplaceAll(m.Sns, " ", ""), ",")
	}
	if len(m.ProductModelInStr) > 0 {
		m.ProductModelIn = strings.Split(strings.ReplaceAll(m.ProductModelInStr, " ", ""), ",")
	}
	if len(m.ProductModelNotInStr) > 0 {
		m.ProductModelNotIn = strings.Split(strings.ReplaceAll(m.ProductModelNotInStr, " ", ""), ",")
	}
	if len(m.DeptIdNot) > 0 {
		m.DeptIdNIn = strings.Split(strings.ReplaceAll(m.DeptIdNot, " ", ""), ",")
	}
	return *m
}
